<template>
  <div class="main-all">
    <p class="p-all"><i class="el-icon-s-platform"></i>系管理</p>
    <div class="main">
      <div class="main-1">
        <el-button icon="el-icon-plus" @click="dialogVisible = true; ADD()">新建</el-button>
        <el-input v-model="input" placeholder="请输入系名"></el-input>
        <el-button type="primary" @click="getPage" style="margin-left: 20px;" icon="el-icon-search">搜索</el-button>
      </div>
      <el-pagination :current-page="page" @current-change="fn2" :current-change="page" background
        layout="prev, pager, next" :total="total" style="margin-bottom: 10px;">
        <!--                                          total页码，设定为变量 -->
      </el-pagination>
      <div style="overflow-y: scroll;width: 100%;height: 80%;">
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="id" label="系ID">
        </el-table-column>
        <el-table-column prop="name" label="系名">
        </el-table-column>
        <el-table-column prop="majorCount" label="专业数">
        </el-table-column>
        <el-table-column prop="teacherCount" label="教师数">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click=" changed(scope.row)">编辑</el-button>
            <el-button type="text" @click=" deletes(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      </div>
    </div>
  </div>


</template>

<script>
import serve from '../api/index.js'
import { getPages, getList, ADDList, changeorno, deleted } from '../api/system.js'
export default {
  data() {
    return {
      input: '',
      total: 100,
      page: 1,
      tableData: [],
      add: '',
      dialogVisible: false,
      dialogVisible1: false,
    }
  },
  methods: {
    // getlist(){
    //   // getList({name:this.input},1).then(res=>{
    //   //   console.log(res);

    //   // })
    //   console.log(this.input);

    //   getPages('测试课程99').then(res=>{
    //     console.log(res);

    //   })
    // },
    getPage() {
      getPages(this.input).then((res) => {
        console.log(res.data, 'res.data');
        this.total = res.data * 10
        if (res.message == 'success') {
          getList({ name: this.input }, this.page).then((res) => {
            console.log(res, 'page-res');
            this.tableData = res.data
          })
        }
      })
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => { });
    },
    fn2(val) {
      this.page = val
      console.log(this.page);
      this.getPage()
    },

    ADD(item) {
      this.$prompt('请输入系名', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(({ value }) => {
        ADDList(value).then((res) => {
          console.log(res);
          this.getPage()
        })
        this.$message({
          type: 'success',
          message: '新增系名: ' + value
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消新增系名'
        });
      });

    },

    changed(item) {
      this.$prompt('请更改系名', '提示', {
        inputValue: item.name,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(({ value }) => {
        changeorno(value, item.id).then((res) => {
          console.log(res);
          this.getPage()
        })
        this.$message({
          type: 'success',
          message: '你改后的系名是: ' + value
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消输入'
        });
      });
    },
    deletes(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleted(id).then((res) => {
          console.log(res);
        })
        this.getPage()
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });

    }

  },
  mounted() {
    this.getPage()
  }
}

</script>

<style scoped>
.el-table {
  width: 100% !important;
}

.main {
  .el-button {
    height: 40px;
  }

  .main-1 {
    height: 60px;
    display: flex;
    align-items: center;
    padding: 10px;
  }

  .el-input {
    height: 30px;
    width: 40%;
    margin-left: 35%;
    margin-bottom: 10px;
  }

}
</style>